using Assets.Scripts.DL.Enemy.CommonEnemy;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EquippedManager : MonoBehaviour
{
    public Sprite nullSprite;

    public WeaponsBackpackData weaponBackpack;
    private WeaponData weaponData;
    public Image weaponImage;

    public ItemsBackpackData itemBackpack;
    private ItemData itemData;
    public Image itemImage;
    public Text itemQuantity;

    private PlayerStateData playerStateData;
    private void Start()
    {
        foreach (var weapon in weaponBackpack.weaponList)
        {
            if(weapon.isEquipped) 
            { 
                weaponData = weapon;
                break;
            }
        }
        foreach (var item in itemBackpack.itemList)
        {
            if (item.isEquipped)
            {
                itemData = item;
                break;
            }
        }
    }

    private void Update()
    {
        if (weaponData == null)
        {
            weaponImage.sprite = nullSprite;
            playerStateData = GameObject.FindGameObjectWithTag(Tags.Player).GetComponent<PlayerController>().StateData;
            playerStateData.BowExAtk = 0;
            playerStateData.SwordExAtk = 0;
        }
        else if (!weaponData.isEquipped)
            weaponData = null;
        else
        {
            playerStateData = GameObject.FindGameObjectWithTag(Tags.Player).GetComponent<PlayerController>().StateData;
            if (weaponData.type == WeaponType.Sword)
            {
                playerStateData.SwordExAtk = weaponData.attack;
                playerStateData.BowExAtk = 0;
            }
            else if (weaponData.type == WeaponType.Bow)
            {
                playerStateData.BowExAtk = weaponData.attack;
                playerStateData.SwordExAtk = 0;
            }
            weaponImage.sprite = weaponData.sprite;
        }


        if (itemData == null)
        {
            itemImage.sprite = nullSprite;
            itemQuantity.text = string.Empty;
        }
        else if (itemData.quantity <= 0)
        {
            itemData.isEquipped = false;
            itemData = null;
        }
        else
        {
            itemImage.sprite = itemData.sprite;
            itemQuantity.text = itemData.quantity.ToString();
            if (Input.GetKeyDown(KeyCode.R))
            {
                ItemsBackpackManager.instance.useItem(itemData);
            }
        }
    }

    public void setWeaponData(WeaponData weaponData)
    {
        this.weaponData = weaponData;
    }
    public void setItemData(ItemData itemData)
    {
        this.itemData = itemData;
    }
}
